package SoftwareEngineering01.WordFrequence.Step1;

import SoftwareEngineering01.WordFrequence.readFile.CompareWith;

import java.util.*;

public class Counting_01 {
    public static Map<String,Integer> getWordMap(String string){
        string = string.toLowerCase();
        Map<String,Integer> map = new HashMap<String,Integer>();
        String list [] = string.split("\\W+");
        int sum = 0;
        for (int i = 0; i < list.length; i++) {
            if(!map.containsKey(list[i])){
                map.put(list[i],1);
            }else{
                Integer num = map.get(list[i]);
                map.put(list[i],num+1);
            }
            sum++;
        }
        return map;
    }

    public static void counting_01(String string){
        Map<String,Integer> map = getWordMap(string);
        List<Map.Entry<String,Integer>> list = new ArrayList<>();
        list.addAll(map.entrySet());
        CompareWith compareWith = new CompareWith();
        Collections.sort(list,compareWith);
        for(Map.Entry<String,Integer> item:list){
            System.out.println(item.getKey()+"出现的次数共有:"+item.getValue());
        }
    }
}
